package com.itheima.dao;

import com.itheima.bean.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.lf5.util.Resource;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.ResourceBundle;

public class StudentTest {
    @Test
    public void delete() throws Exception{
        InputStream resourceAsStream = Resources.getResourceAsStream("myBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = build.openSession();
        int delete = sqlSession.delete("myBatisStudent.delete", 4);
        sqlSession.commit();
        System.out.println(delete);
        resourceAsStream.close();
        sqlSession.close();



    }



    @Test
    public void update() throws Exception{
        InputStream resourceAsStream = Resources.getResourceAsStream("myBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = build.openSession();

        Student student=new Student(3,"王八",28);
        int update = sqlSession.update("myBatisStudent.update", student);

        sqlSession.commit();

        System.out.println(update);
        resourceAsStream.close();
        sqlSession.close();


    }





    @Test
    public void insert() throws Exception{
        InputStream resourceAsStream = Resources.getResourceAsStream("myBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = build.openSession();
        Student student=new Student(4,"赵六",26);
        int myBatisStudent = sqlSession.insert("myBatisStudent.insert", student);


        sqlSession.commit();
        System.out.println(myBatisStudent);

        resourceAsStream.close();
        sqlSession.close();




    }



    @Test
    public void selectBIDC() throws Exception{
        InputStream resourceAsStream = Resources.getResourceAsStream("myBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = build.openSession();
        Student o = sqlSession.selectOne("myBatisStudent.selectBIDC",2);
        System.out.println(o);
        resourceAsStream.close();
        sqlSession.close();


    }

    @Test
    public void selectAll() throws Exception {
        InputStream resourceAsStream = Resources.getResourceAsStream("myBatisConfig.xml");

        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);

        SqlSession sqlSession = build.openSession();

        List<Student> objects = sqlSession.selectList("myBatisStudent.selectAll");

        for (Student object : objects) {
            System.out.println(object);
        }
        sqlSession.close();
        resourceAsStream.close();
    }

}

